/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2012 Medavie BlueCross. All rights reserved.
 * 
 * The contents of this file are subject to the Medavie Blue Cross
 * Application Code terms of use policy, for more information contact
 * Medavie Blue Cross.
 */

package com.binkam.merashehar.admin;

import com.binkam.merashehar.common.database.MSHibernateUtil;
import com.binkam.merashehar.constants.MSConstants;
import com.binkam.merashehar.dto.Country;
import com.binkam.merashehar.dto.State;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author sunchak
 */
public class StateHandler extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        MSHibernateUtil hibernateUtil = new MSHibernateUtil();
        Session hibernateSession = hibernateUtil.getSessionFactory().openSession();
        try {
            String stateName = request.getParameter("statename");
            String countryid = request.getParameter("countryid");
            String flag = request.getParameter("dbflag");
            
            State tempState = new State();
            
            
            
            if(Integer.parseInt(flag)==MSConstants.DB_OPERATION_ADD){
                Transaction hibernateTransaction =  hibernateSession.beginTransaction();
                tempState.setStatename(stateName);
                Country tempCountry = new Country();
                tempCountry.setCountryname("India");
                tempCountry.setCountryid(new Integer(countryid));                
                tempState.setCountry(tempCountry);
                hibernateSession.save(tempState);
                hibernateSession.flush();
                hibernateTransaction.commit();
                out.println("Successfully inserted State :::");
                response.sendRedirect("/app/admin/index.jsp");
                
            }else if(Integer.parseInt(flag)==MSConstants.DB_OPERATION_UPDATE){
                Transaction hibernateTransaction =  hibernateSession.beginTransaction();
                tempState.setStatename(stateName);
                hibernateSession.update(tempState);
                hibernateSession.flush();
                hibernateTransaction.commit();
                out.println("Successfully inserted State :::");
                response.sendRedirect("/app/admin/index.jsp");
                
            }else if(Integer.parseInt(flag)==MSConstants.DB_OPERATION_SELECT){
                String sql ="from State";
                Query query = hibernateSession.createQuery(sql);
                List queryList = query.list();
                request.getSession(true).setAttribute("StateList", queryList);   
                out.println("State :::"+queryList.size());
                
            }
            
            
        } catch(Exception e) { 
            e.printStackTrace();
        }finally{
            out.close();
            hibernateSession.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
